<?php

/*

Searching Shops (search_shops.pro.php)

*/
ob_start();
$page_title = "Searching Shops";
$rank_check = 1;
include "header.inc.php";

$findQuest = fetch("SELECT * FROM quests WHERE game = '$game' AND user = '$userid'");
if ($findQuest[id])
{
	die("$openHTML<p>Sorry, search shops is disabled while you are in a <a href=quests.php?game=$game>quest</a>!</p>$closeHTML");
}

if ($search_type == "exact")
{
	$findItem2 = fetch("SELECT * FROM items2 WHERE item_name = '$search_for' AND game = '$game'");
	$findResults = mysql_query("SELECT * FROM usershops_items2 WHERE price > '0' AND item_id = '$findItem2[id]' AND game = '$game' ORDER BY price ASC LIMIT 100");
}

if ($search_type == "contain")
{
	$numRandomItem = mysql_num_rows(mysql_query("SELECT id FROM items2 WHERE item_name LIKE '%$search_for%' AND game = '$game'")) - 1;
	$randomItem = rand(0,$numRandomItem);
	$findItem2 = fetch("SELECT * FROM items2 WHERE item_name LIKE '%$search_for%' AND game = '$game' ORDER BY id DESC LIMIT $randomItem,1");
	$findResults = mysql_query("SELECT * FROM usershops_items2 WHERE price > '0' AND item_id = '$findItem2[id]' AND game = '$game' ORDER BY price ASC LIMIT 100");
}

while ($getResults = mysql_fetch_array($findResults))
{
	$get_owner = fetch("SELECT username,display_name,rank FROM members2 WHERE id = '$getResults[owner]' AND game = '$game'");
	if ($get_owner[rank] != 0)
	{
		$cellcolor = alternate($cellcolor,"$middleBG","#FFFFFF");

	   	$itemList .= "<TR BGCOLOR=$cellcolor>
	      <TD WIDTH=250>
	         <CENTER><a 	href=usershop.php?game=$game&user=$get_owner[username]>$get_owner[display_name]</a></CENTER>
	      </TD>
	      <TD WIDTH=100>
	         <CENTER>$getResults[stock]</CENTER>
	      </TD>
	      <TD WIDTH=100>
	         <CENTER>$getResults[price]</CENTER>
	      </TD>
	   </TR>";
	}
}

$numResults = mysql_num_rows($findResults);
if ($numResults == 0)
{
   $itemList = "<TR BGCOLOR=$cellcolor>
      <TD WIDTH=250 COLSPAN=3>
      <p align=center><i>Sorry, there were no search results.</i></p>
      </TD>
   </TR>";
}

print "$openHTML";

ECHO <<<END

<p align=center><img src=images/user_images/opg_$game/items/item_$findItem2[id].gif><br>Searching for <b>$findItem2[item_name]</b> found $numResults results!</p>

<CENTER><TABLE BGCOLOR="$tableOutline" CELLSPACING=1 CELLPADDING=0 WIDTH=450>
   <TR BGCOLOR="$topAndBottomBG">
      <TD COLSPAN=3 WIDTH=450 HEIGHT=50>
         <CENTER class=white><FONT SIZE="+2" COLOR="$topAndBottomText"><B>--Searching
         Shops--</B></FONT></CENTER>
      </TD>
   </TR>

	$itemList

</TABLE>
</CENTER>

END;

print "$closeHTML";
?>